﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using System.Windows.Interactivity;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Tasks;

namespace Eaglight.Behaviors
{
    public class IdentifyBehavior: Behavior<Map>
    {
        private IdentifyTask itask;
        
        public event EventHandler<IdentifyEventArgs> IdentifyCompleted;
        public event EventHandler<TaskFailedEventArgs> IdentifyFailed;


        
        protected override void OnAttached()
        {
            AssociatedObject.MouseClick += AssociatedObject_MouseClick;
            initTasks();
        }

        protected override void OnDetaching()
        {
            AssociatedObject.MouseClick -= AssociatedObject_MouseClick;
        }

        private void AssociatedObject_MouseClick(object sender, Map.MouseEventArgs e)
        {
            identify(e.MapPoint);
        }
        
        private void identify(MapPoint p)
        {
            // create task parameters
            IdentifyParameters idparams = new IdentifyParameters()
            {
                Geometry = p,
                MapExtent = AssociatedObject.Extent,
                Height = (int)AssociatedObject.ActualHeight,
                Width = (int)AssociatedObject.ActualWidth,
                LayerOption = this.IdentifyLayerOptions
            };

            // create the task
            Layer l = AssociatedObject.Layers[IdentifyLayerId];
            ArcGISDynamicMapServiceLayer dml = l as ArcGISDynamicMapServiceLayer;
            ArcGISTiledMapServiceLayer tml = l as ArcGISTiledMapServiceLayer;
            string url = dml != null ? dml.Url : tml != null ? tml.Url : "";
            if (url == "")
                throw new Exception("Layer is null or not supported");

            itask.Url = url;
            itask.ExecuteAsync(idparams);
        }

        private void initTasks()
        {
            itask = new IdentifyTask();
            itask.ExecuteCompleted += new EventHandler<IdentifyEventArgs>(itask_ExecuteCompleted);
            itask.Failed += new EventHandler<TaskFailedEventArgs>(itask_Failed);
        }

        private void itask_Failed(object sender, TaskFailedEventArgs e)
        {
            if (IdentifyFailed != null)
            {
                IdentifyFailed(this, e);
            }
        }

        private void itask_ExecuteCompleted(object sender, IdentifyEventArgs e)
        {
            if (IdentifyCompleted != null)
                IdentifyCompleted(this, e);

            
        }

        public static readonly DependencyProperty IdentifyLayerOptionProperty = DependencyProperty.Register("IdentifyLayerOptions", typeof(LayerOption), typeof(IdentifyBehavior), null);
        public static readonly DependencyProperty IdentifyLayerIdProperty = DependencyProperty.Register("IdentifyLayerId", typeof(string), typeof(IdentifyBehavior), null);
        

        public LayerOption IdentifyLayerOptions
        {
            get
            {
                return (LayerOption)GetValue(IdentifyLayerOptionProperty);
            }
            set
            {
                SetValue(IdentifyLayerOptionProperty, value);
            }
        }

        public string IdentifyLayerId
        {
            get
            {
                return (string)GetValue(IdentifyLayerIdProperty);
            }

            set
            {
                SetValue(IdentifyLayerIdProperty, value);
            }

        }
    }
}
